package com.car.android.sdk.manager;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.applovin.sdk.AppLovinSdkSettings;
import com.car.android.sdk.Constant;
import com.car.android.sdk.ad.CarVideoIAP;
import com.car.android.sdk.kwai.CarKwaiReward;
import com.car.android.sdk.max.CarMaxReward;
import com.car.android.sdk.utils.CarAesUtils;
import com.car.android.sdk.utils.CarSPUtils;
import com.kwai.network.sdk.KwaiAdSDK;
import com.kwai.network.sdk.api.KwaiInitCallback;
import com.kwai.network.sdk.api.SdkConfig;
import com.kwai.network.sdk.core.KwaiCustomController;

public class CarVideoManger {

    public static void initMediation(Context context) {
        //AppLovinPrivacySettings
        AppLovinPrivacySettings.setHasUserConsent(true, context);
        AppLovinPrivacySettings.setDoNotSell(false, context);
        //AppLovinSdkSettings
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(context).getSettings();
        //settings.setVerboseLogging(true);
        settings.setExtraParameter("enable_black_screen_fixes", "true");
        // Create the initialization configuration
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        CarAesUtils.decrypt(Constant.MAX_KEY, Constant.REQUEST_HEADER_FLAG), context)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(context).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("CCMatchW", "onSdkInitialized");
                CarMaxReward.getInstance().loadRewardAd(context);

                String country = CarSPUtils.getString(Constant.KEY_COUNTRY,"");
                if (config != null && !TextUtils.isEmpty(config.getCountryCode())
                        && TextUtils.isEmpty(country)) {
                    Log.e("CCMatchW", config.getCountryCode());
                    CarSPUtils.putString(Constant.KEY_COUNTRY,config.getCountryCode());
                }
            }
        });

        KwaiAdSDK.init(context, new SdkConfig.Builder()
                // appId，必填，请联系KSIB_KwaiNetwork@kuaishou.com申请正式AppId
                .appId(Constant.KWAI_APP_ID)
                // token，必填，请联系KSIB_KwaiNetwork@kuaishou.com申请正式token，必填
                .token(CarAesUtils.decrypt(Constant.KWAI_TOKEN, Constant.REQUEST_HEADER_FLAG))
                // 非必填，支持debug模式查看日志信息，接入方正式版本需要关闭
                .debug(Constant.KWAI_DEBUG)
                .customController(new KwaiCustomController() {
                    @Override
                    public boolean canReadInstalledPackages() {
                        return false;
                    }
                })
                // sdk初始化回调
                .setInitCallback(new KwaiInitCallback() {
                    @Override
                    public void onSuccess() {
                        // 初始化成功 load
                        Log.d("CCMatchW", "KwaiAdSDK init success");
                        String country = CarSPUtils.getString(Constant.KEY_COUNTRY,"");
                        if (Constant.COUNTRY_ID.equals(country) || Constant.COUNTRY_BR.equals(country)) {
                            CarKwaiReward.getInstance().loadRewardAd();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        // 初始化失败，如果初始化失败，还去尝试获取SDK的能力，会出现异常，请确保初始化成功调用你需要的方法
                        // 错误码详见第6章节
                        Log.d("CCMatchW", "KwaiAdSDK init fail " + code + ":" + msg);
                    }
                }).build());

    }

    public static CarVideoIAP getReward1Instance() {
        return CarMaxReward.getInstance();
    }

    public static CarVideoIAP getReward2Instance() {
        return CarKwaiReward.getInstance();
    }
}
